package cn.iocoder.yudao.module.forward.controller.admin.testsend;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.testsend.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.testsend.TestSendDO;
import cn.iocoder.yudao.module.forward.service.testsend.TestSendService;

@Tag(name = "管理后台 - 演练发布")
@RestController
@RequestMapping("/forward/test-send")
@Validated
public class TestSendController {

    @Resource
    private TestSendService testSendService;

    @PostMapping("/create")
    @Operation(summary = "创建演练发布")
    @PreAuthorize("@ss.hasPermission('forward:test-send:create')")
    public CommonResult<Integer> createTestSend(@Valid @RequestBody TestSendSaveReqVO createReqVO) {
        return success(testSendService.createTestSend(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新演练发布")
    @PreAuthorize("@ss.hasPermission('forward:test-send:update')")
    public CommonResult<Boolean> updateTestSend(@Valid @RequestBody TestSendSaveReqVO updateReqVO) {
        testSendService.updateTestSend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除演练发布")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:test-send:delete')")
    public CommonResult<Boolean> deleteTestSend(@RequestParam("id") Integer id) {
        testSendService.deleteTestSend(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得演练发布")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:test-send:query')")
    public CommonResult<TestSendRespVO> getTestSend(@RequestParam("id") Integer id) {
        TestSendDO testSend = testSendService.getTestSend(id);
        return success(BeanUtils.toBean(testSend, TestSendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得演练发布分页")
    @PreAuthorize("@ss.hasPermission('forward:test-send:query')")
    public CommonResult<PageResult<TestSendRespVO>> getTestSendPage(@Valid TestSendPageReqVO pageReqVO) {
        PageResult<TestSendDO> pageResult = testSendService.getTestSendPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TestSendRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出演练发布 Excel")
    @PreAuthorize("@ss.hasPermission('forward:test-send:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTestSendExcel(@Valid TestSendPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TestSendDO> list = testSendService.getTestSendPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "演练发布.xls", "数据", TestSendRespVO.class,
                        BeanUtils.toBean(list, TestSendRespVO.class));
    }

}